import os
from building import *

cwd = GetCurrentDir()

Import('asenv')
ASROOT = asenv['ASROOT']
BDIR = asenv['BDIR']
MODULES = ['ECUM','SCHM','MCU',
           'DET',
           'TRACE',
           'CLIB_STDIO_PRINTF',
           'ASKAR','SHELL','CLIB_STRTOK_R',
           'MPC5634M_MLQB80',
           'PORT','DIO','COMMONXML'
           ]

print('INFO: set NO_COM=YES to remove COM stack')
if(os.getenv('NO_COM') != 'YES'):
    MODULES += ['CAN','CANIF','PDUR','COM','COMM','CANTP',
                'DCM','CANNM','CANSM','NM','OSEKNM','XCP']

# post special process for asboot release
if(asenv['RELEASE']=='asboot'):
    MODULES = ['ECUM','SCHM','MCU','DET',
               'PORT','CAN','CANIF','PDUR','CANTP','XCP',
               'DCM','FLASH','TINYOS',
               'CLIB_STDIO_PRINTF','CLIB_STRTOK_R',
               'SHELL','FLASH_CMD','MEM_CMD','JMP_CMD',
               'MPC5634M_MLQB80','BOOTLOADER'
               ]
    asenv.Append(CPPDEFINES=['CMDLINE_MAX=4096','FLASH_CMD_MAX_DATA=2000'])

ARCH='mpc56xx'
arch='mpc56xx'

if(asenv['MODULES'] is None):
    asenv['MODULES'] = MODULES
else:
    MODULES = asenv['MODULES']

asenv['ARCH'] = ARCH
asenv['arch'] = arch

objs = []

asenv.Append(LINKFLAGS=['-map','%s/mpc56xx.map'%(BDIR)])

MKDir('%s/common/'%(cwd))
MKSymlink('%s/../board.posix/common/Mcu_Cfg.h'%(cwd),'%s/common/Mcu_Cfg.h'%(cwd))
MKSymlink('%s/../board.posix/common/Mcu_Cfg.c'%(cwd),'%s/common/Mcu_Cfg.c'%(cwd))

asenv.Append(CPPPATH=['%s/common'%(cwd)])

if('CAN' in MODULES):
    objs += Glob('common/Can*.c')
    asenv.Append(CPPDEFINES=['NOT_PYGEN_CAN'])

if('DIO' in MODULES):
    objs += Glob('common/Dio*.c')

if('MCU' in MODULES):
    objs += Glob('common/Mcu*.c')

if('PORT' in MODULES):
    objs += Glob('common/Port*.c')

if('SHELL' in MODULES):
    asenv.Append(CPPDEFINES=['ENABLE_SHELL_ECHO_BACK'])

pkg = Package('https://github.com/as-boards/mpc56xx.git')
MKSymlink(pkg+'/MPC5634M_MLQB80',cwd+'/MPC5634M_MLQB80')
if('MPC5634M_MLQB80' in MODULES):
    srcs = Glob('MPC5634M_MLQB80/Project/src/*.c')
    SrcRemove(srcs, ['main.c','MPC55xx_init_debug.c'])
    objs += srcs
    asenv.Append(LINKFLAGS=['-lcf','%s/MPC5634M_MLQB80/Project/lcf/MPC5634M_MLQB80.lcf'%(cwd)])
    asenv.Append(CPPPATH=['%s/MPC5634M_MLQB80/Project/src'%(cwd)])
    MKSymlink('%s/MPC5634M_MLQB80/Project/src/MPC5634M_MLQB80.h'%(cwd),
              '%s/MPC5634M_MLQB80/Project/src/mpc56xx.h'%(cwd))
    asenv.Append(CPPDEFINES=['CFG_MPC563XM','CFG_MPC55XX'])

asenv.Append(CPPDEFINES=['CPU_FREQUENCY=64'])

Return('objs')
